package com.cyx.friend.controller.user;

import com.cyx.common.core.constants.HttpConstants;
import com.cyx.common.core.controller.BaseController;
import com.cyx.common.core.domain.LoginUserVO;
import com.cyx.common.core.domain.R;
import com.cyx.friend.domain.user.dto.UserDTO;
import com.cyx.friend.domain.user.dto.UserEditDTO;
import com.cyx.friend.domain.user.vo.UserVO;
import com.cyx.friend.service.user.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Tag(name = "C端用户接口")
public class UserController extends BaseController {

    @Autowired
    private IUserService userService;

    @Operation(description = "发送验证码")
    @PostMapping("/sendCode")
    public R<Void> sendCode(@RequestBody UserDTO userDTO) {
        return toR(userService.sendCode(userDTO));
    }

    @Operation(description = "用户登录")
    @PostMapping("/code/login")
    public R<String> codeLogin(@RequestBody UserDTO userDTO) {
        return R.ok(userService.codeLogin(userDTO.getPhone(), userDTO.getCode()));
    }

    @DeleteMapping("/logout")
    public R<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        return toR(userService.logout(token));
    }

    @GetMapping("/info")
    @Operation(summary = "获取用户昵称", description = "根据token获取用户昵称")
    public R<LoginUserVO> info(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        return userService.info(token);
    }

    @Operation(summary = "获取用户详情信息", description = "获取用户信息")
    @GetMapping("/detail")
    public R<UserVO> detail() {
        return R.ok(userService.detail());
    }

    @Operation(summary = "编辑用户信息", description = "根据传入的参数修改用户信息")
    @PutMapping("/edit")
    public R<Void> edit(@RequestBody UserEditDTO userEditDTO) {
        return toR(userService.edit(userEditDTO));
    }

    @PutMapping("/head-image/update")
    public R<Void> updateHeadImage(@RequestBody UserEditDTO userEditDTO) {
        return toR(userService.updateHeadImage(userEditDTO.getHeadImage()));
    }
}
